import { login, logout, getAllInfo } from '@/api/user'
import { getToken, setToken, removeToken } from '@/utils/auth'
import { resetRouter } from '@/router'

const getDefaultState = () => {
  return {
    token: getToken(),
    name: '',
    userId: '',
    userPwd: '',
    userName: '',
    userType: '',
    userBranch: '',
    loginmsg: '',
    avatar: ''

  }
}

const state = getDefaultState()

const mutations = {
  RESET_STATE: (state) => {
    Object.assign(state, getDefaultState())
  },
  SET_TOKEN: (state, token) => {
    state.token = token
  },
  SET_NAME: (state, name) => {
    state.name = name
  },
  SET_USERID: (state, userId) => {
    state.userId = userId
  },
  SET_USERPWD: (state, userPwd) => {
    state.userPwd = userPwd
  },
  SET_USERTYPE: (state, userType) => {
    state.userType = userType
  },
  SET_USERNAME: (state, userName) => {
    state.userName = userName
  },
  SET_USERBRANCH: (state, userBranch) => {
    state.userBranch = userBranch
  },
  SET_LOGINMSG: (state, loginmsg) => {
    state.loginmsg = loginmsg
  },
  SET_AVATAR: (state, avatar) => {
    state.avatar = avatar
  }
}

const actions = {
  // user login
  login({ commit }, userInfo) {
    const { userId, userPwd } = userInfo
    return new Promise((resolve, reject) => {
      login({ userId: userId.trim(), userPwd: userPwd }).then(response => {
        const data = response
        const loginmsg = response.msg
        // 设置 token，作为用户已登陆的前端标识，存在 cookie 中
        if (loginmsg !== '操作成功') {
          commit('SET_LOGINMSG', data.msg)
        } else {
          commit('SET_LOGINMSG', data.msg)
          commit('SET_TOKEN', data.obj.userType)
          commit('SET_USERID', data.obj.userId)
          sessionStorage.setItem('userStatus', data.obj.userStatus)
          sessionStorage.setItem('userId', data.obj.userId)
          sessionStorage.setItem('flist', 'nolook')
          sessionStorage.setItem('slist', 'nolook')
          sessionStorage.setItem('tlist', 'nolook')
          sessionStorage.setItem('reloadUserInfo', 2)
          if (data.obj.userType === '' || data.obj.userType === null) {
            this.$message.error('当前用户无登录权限')
          } else {
            setToken(data.obj.userType)
            var user_Id = response.obj.userId
            sessionStorage.setItem('user_Id', user_Id)
          }
        }
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },

  // get user info
  getInfo({ commit, state }) {
    return new Promise((resolve, reject) => {
      getAllInfo(state.userId).then(response => {
        const data = response
        if (!data) {
          return reject('Verification failed, please Login again.')
        }
        commit('SET_NAME', data.obj.userInfo.userName)
        commit('SET_USERPWD', data.obj.userPwd)
        commit('SET_USERNAME', data.obj.userInfo.userName)
        commit('SET_USERTYPE', data.obj.userType)
        commit('SET_USERBRANCH', data.obj.userBranch)
        commit('SET_AVATAR', '../../../public/favicon.ico')
        resolve(data)
      }).catch(error => {
        reject(error)
      })
    })
  },

  // user logout
  logout({ commit, state }) {
    global.antRouter = ''
    return new Promise((resolve, reject) => {
      logout(state.token).then(() => {
        removeToken() // must remove  token  first
        resetRouter()
        commit('RESET_STATE')
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },

  // remove token
  resetToken({ commit }) {
    return new Promise(resolve => {
      removeToken() // must remove  token  first
      commit('RESET_STATE')
      resolve()
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}

